<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	  xmlns:th="http://www.thymeleaf.org"
	  lang="zh-cn">
<head>
	<meta content="云之讯,云通讯,云通讯平台" name="keywords"/>
	<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
	<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
	<title>短信云平台运营系统 - 品牌代理商订单</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
	<link th:href="@{/resources/css/bootstrap.min14ed.css?v=3.3.6}" href="/resources/css/bootstrap.min14ed.css" rel="stylesheet">
	<link th:href="@{/resources/css/font-awesome.min93e3.css?v=4.4.0}" href="/resources/css/font-awesome.min93e3.css" rel="stylesheet"><!-- jqgrid-->
	<link th:href="@{/resources/js/plugins/jqgrid/css/ui.jqgrid-bootstrap.css}" href="/resources/js/plugins/jqgrid/css/ui.jqgrid-bootstrap.css" rel="stylesheet">
	<link th:href="@{/resources/css/animate.min.css}" href="/resources/css/animate.min.css" rel="stylesheet">
	<link th:href="@{/resources/css/style.min862f.css?v=4.1.0}" href="/resources/css/style.min862f.css" rel="stylesheet">
	<link th:href="@{/resources/css/plugins/sweetalert/sweetalert.css}" href="/resources/css/plugins/sweetalert/sweetalert.css" rel="stylesheet">
	<style>
		/* Additional style to fix warning dialog position */
		#alertmod_table_list_2 {
			top: 900px !important;
		}
	</style>
</head>
<body class="gray-bg"><div th:replace="stat :: statCode"></div>
<div class="wrapper wrapper-content  animated fadeInRight">
	<div class="row">
		<div class="col-sm-12">
			<div class="tabs-container">
				<ul class="nav nav-tabs">
					<li class="active" id="agentTab" th:if="${menus.PINdlsdd}"><a data-toggle="tab" href="#tab-1" aria-expanded="true"> 品牌代理商订单</a>
					</li>
					<li class="" th:if="${menus.OEMdlsdd}"><a id="OEMdlsdd"  href="/finance/oem/agentOrder?tab=1">客户订单</a>
					</li>
					<li class="" th:if="${menus.OEMkhdd}"><a id="OEMkhdd"   href="/finance/oem/agentOrder?tab=2">子账户订单</a>
					</li>
					<li class="" th:if="${menus.OEMdlskc}"><a id="OEMdlskc"  href="/finance/oem/agentInventory?tab=1">客户库存</a>
					</li>
					<li class="" th:if="${menus.OEMkhkc}"><a id="OEMkhkc"  href="/finance/oem/agentInventory?tab=2">子账户库存</a>
					</li>
				</ul>

				<div class="tab-content">
					<div id="tab-1" class="tab-pane active" th:if="${menus.PINdlsdd}">
						<div class="ibox-content">
							<form class="form-inline" method="post" id="mainForm" >
								<div class="form-group m-r-md">
									<!--<input type="text" placeholder="订单号/产品ID/客户ID/代理商ID" name="condition" id="condition" class="form-control">-->
									<input type="text" placeholder="订单号/产品代码/客户ID/代理商ID" name="condition" id="condition" class="form-control" style="width: 246px;">
								</div>
								<div class="form-group">
									<label for="operatorCode">运营商类型:</label>
									<select id="operatorCode" name="operatorCode" class="form-control ">
										<option value="">全部</option>
										<option value="1">移动</option>
										<option value="2">联通</option>
										<option value="3">电信</option>
										<option value="0">全网</option>
										<option value="4">国际</option>
									</select>
								</div>
								<div class="form-group">
									<label for="status">订单状态:</label>
									<select id="status" name="status"  class="form-control">
										<option value="">全部状态</option>
										<option value="0">待审核</option>
										<option value="1" selected>订单生效</option>
										<option value="2">订单完成</option>
										<option value="3">订单失败</option>
										<option value="4">订单取消</option>
									</select>
								</div>

								<!-- 时间选择 -->
								<div class="form-group" id="sandbox-container" style="margin-left:2.5%">
									<div class="form-group pull-right">创建时间:
										<input placeholder="开始时间" class="form-control layer-date" name="startDateTime" id="startDateTime" />至
										<input placeholder="结束时间" class="form-control layer-date" name="endDateTime" id="endDateTime" />
									</div>
								</div>
								<div class="form-group">
									<span>&nbsp;&nbsp;<button type="button" class="btn btn-sm btn-danger" onclick="search()">搜索</button></span>
									<span>&nbsp;&nbsp;<button type="button" class="btn btn-sm btn-primary" onclick="exportExcel()">导出Excel</button></span>
								</div>
							</form>
						</div>
						<div class="ibox-content">
							<div class="jqGrid_wrapper">
								<table id="table_list"></table>
								<div id="pager_list"></div>
							</div>
							<p>&nbsp;</p>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>



</div>
<script th:src="@{/resources/js/jquery.min.js?v=2.1.4}"></script>
<script th:src="@{/resources/js/bootstrap.min.js?v=3.3.6}"></script>
<script th:src="@{/resources/js/content.min.js?v=1.0.0}"></script>
<script th:src="@{/resources/js/layer/layer.js}"></script>
<script th:src="@{/resources/js/plugins/layer/laydate/laydate.js}"></script>
<script th:src="@{/resources/js/plugins/peity/jquery.peity.min.js}"></script>
<script th:src="@{/resources/js/plugins/jqgrid/js/i18n/grid.locale-cn.js}"></script>
<script th:src="@{/resources/js/plugins/jqgrid/js/jquery.jqGrid.min.js}"></script>
<script th:src="@{/resources/js/date_format.min.js?v=1.0.0}"></script>
<script th:src="@{/resources/js/plugins/sweetalert/sweetalert.min.js}"></script>
<script th:src="@{/resources/js/common.js?v=1.0.0}"></script>

<script th:inline="javascript">
    var max_export_num = [[${max_export_num}]];
    var menus = [[${menus}]];
    if (menus.PINdlsdd) {

    } else if (menus.OEMdlsdd) {
        $("#OEMdlsdd")[0].click();
    } else if (menus.OEMkhdd) {
        $("#OEMkhdd")[0].click();
    } else if (menus.OEMdlskc) {
        $("#OEMdlskc")[0].click();
    } else if (menus.OEMkhkc) {
        $("#OEMkhkc")[0].click();
    }
    // $("#startDateTime").val(laydate.now(-1));
    // $("#end_tim_day").val(laydate.now(-1));

    var start = {
        elem: "#startDateTime",
        format: "YYYY-MM-DD",
    	min: halfYearAgo("yyyy-MM-dd"),
        max: laydate.now(),
        istoday: true,
        choose: function (datas) {
            end.min = datas;
        }
    };

    var end = {
        elem: "#endDateTime",
        format: "YYYY-MM-DD",
    	min: halfYearAgo("yyyy-MM-dd"),
        max: laydate.now(),
        istoday: true,
        choose: function (datas) {
            start.max = datas;
        }
    };
    laydate(start);
    laydate(end);

    function search(){
        $("#table_list").jqGrid('setGridParam',{
            datatype:'json',
            postData: {
                condition: $("#condition").val(),
                status:$("#status").val(),
                operatorCode:$("#operatorCode").val(),
                startDateTime: $("#startDateTime").val(),
                endDateTime: $("#endDateTime").val(),
            }, //发送数据
        }).trigger("reloadGrid"); //重新载入
    }

    // 导出Excel文件
    function exportExcel() {
        var totalCount = $("#table_list").jqGrid('getGridParam','records');
        if (totalCount == 0) {
            layer.alert("共0条记录，导出Excel文件失败");
            return;
        }
        if(totalCount > max_export_num){
            layer.msg("导出Excel文件条数大于"+max_export_num+"条", {icon: 2});
            return;
        }
        var mainForm = $("#mainForm");
        var action = mainForm.attr("action");
        var exporUrl = /*[[@{/finance/order/export}]]*/'';
        mainForm.attr("action", exporUrl).submit();
        mainForm.attr("action", action);
    }

    function getDetail(sub_id){
        var url = /*[[@{/finance/order/view}]]*/;
        window.location.href = url+"?sub_id="+sub_id;
    }

    function payment(sub_id,order_id,quantity,client_id,agent_id){
        var info = " 订单号："+order_id+"<br>金额："+quantity+"<br>客户："+client_id+"<br>代理商："+agent_id+"<br><br>购买该订单?"
        swal({
            title: "",
            text: info,
            type: "info",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "购买",
            cancelButtonText: "让我再考虑一下…",
            closeOnConfirm: false,
            html: true
        }, function () {
            $.ajax({
                url:/*[[@{/finance/order/payment}]]*/'',
                type:"POST",
                data:{
                    sub_id: sub_id
                },
                success:function(data){
                    if(data == null){
                        swal("失败！", "系统错误，请稍后再试！", "error")
                    }else if(data.success){
                        swal("成功！", data.msg, "success")
                    }else{
                        swal("失败！", data.msg, "error")
                    }
                    search();
                }
            });
        });
    };

    $(document).ready(function () {
        $.jgrid.defaults.styleUI = "Bootstrap";
        $("#table_list").jqGrid({
        	url:/*[[@{/finance/order/list}]]*/"",
            mtype: 'POST',
            datatype: "json",
            jsonReader : {
                root:"entityList",
                page: "currentPage",
                total: "totalPage",
                records: "totalCount"
            },
            postData: {
                condition: $("#condition").val(),
                status:$("#status").val(),
                operatorCode:$("#operatorCode").val(),
                startDateTime: $("#startDateTime").val(),
                endDateTime: $("#endDateTime").val(),
            }, //发送数据
            height: 'auto',
            rownumbers:true,
            autowidth: true,
            shrinkToFit: false,
            autoScroll: true,
            rowNum: 20,
            rowList: [10, 20, 30,50],
//            colNames: ["订单号","订单类型","产品ID","产品类型","产品总售价","产品总成本价","总数量","剩余数量","客户ID","代理商ID","状态","有效期（天）","到期时间","创建时间","操作"],
            colModel: [
				{label:'订单号',name: "orderId",align: "left",sortable:false},
				{label:'订单类型',name: "orderTypeStr",align: "left",sortable:false},
				{label:'产品代码',name: "productCode",align: "left",sortable:false},
				{label:'产品类型',name: "productTypeStr",align: "left",sortable:false},
				{label:'运营商',name: "operatorCodeStr",align: "left",sortable:false},
				{label:'区域',name: "areaCodeStr",align: "left",sortable:false},
				{label:'产品总售价',name: "salePriceStr",align: "left",sortable:false},
				{label:'产品总成本价',name: "productCostStr",align: "left",sortable:false},
				{label:'总数量',name: "quantityStr",align: "left",sortable:false},
				{label:'剩余数量',name: "remainQuantityStr",align: "left",sortable:false},
				{label:'客户ID',name: "clientId",align: "left",sortable:false},
				{label:'代理商ID',name: "agentId",align: "left",sortable:false},
				{label:'状态',name: "statusStr",align: "left",sortable:false},
				{label:'有效期（天）',name: "activePeriodStr",align: "left",sortable:false},
				{label:'到期时间',name: "endTimeStr",align: "left",sortable:false},
				{label:'创建时间',name: "createTimeStr",align: "left",sortable:false},
				{label:'操作',name: "total",index: "total",align: "left",sortable:false,title:false,
					formatter:function(cellvalue, options, rowObject){
						var order_id = rowObject.orderId;

						if(order_id!='总计'){
							var _product_type = rowObject.productType;
							var _agent_type = rowObject.agentType;
							var _agent_id = rowObject.agentId;
							var _status = rowObject.status;
	                        var _sub_id = rowObject.subIdStr;
                            var _order_id = rowObject.orderId;
	                        var _client_id = rowObject.clientId;

							var switcher = '';
	                        switcher = "<button type='button' class='btn btn-link btn-xs ' onclick=\"getDetail('"+_sub_id+"')\" title='详情'>详情</button>";
							if (_product_type == 2){
	                            if ((_agent_type == 2 || _agent_type == 1 ) && _status == 0) {
                                    var _quantity = rowObject.quantity;
	                                switcher += "<button type='button' class='btn btn-link btn-xs'  onclick=\"payment('"+_sub_id+"','"+_order_id+"','"+_quantity+"','"+_client_id+"','"+_agent_id+"')\" title='购买'>购买</button>";
								}
							} else {
	                            if ((_agent_type == 2 || _agent_type == 1 ) && _status == 0) {
	                                var _sale_price = rowObject.salePrice;
	                                switcher += "<button type='button' class='btn btn-link btn-xs'  onclick=\"payment('"+_sub_id+"','"+_order_id+"','"+_sale_price+"','"+_client_id+"','"+_agent_id+"')\" title='购买'>购买</button>";
	                            }
	                        }
							return switcher;
						}
						return cellvalue;
					}
				}
            ],
            pager: "#pager_list",
            viewrecords: true,
            hidegrid: false,
            /*统计功能 */  
            footerrow:true,
            gridComplete:function(){
            	var sale_price = 0;
            	var product_cost = 0;
            	var quantity = 0;
            	var remain_quantity = 0;
            	$.ajax({
        			type : "post",
        			url : /*[[@{/finance/order/total}]]*/'',
        			data : {
                        condition: $("#condition").val(),
                        status:$("#status").val(),
                        operatorCode:$("#operatorCode").val(),
                        startDateTime: $("#startDateTime").val(),
                        endDateTime: $("#endDateTime").val()
        			},
        			async:false,
        			success : function(result) {
        				if(result.success){
        					sale_price = result.data.sale_price.toFixed(2);
        					product_cost = result.data.product_cost.toFixed(2);
        					quantity = result.data.quantity;
        					remain_quantity = result.data.remain_quantity;
        				}else{
        					layer.msg("计算总计失败", {icon: 2,time: msgTime}); 
        				}
        				
        			}
        		});
            	$("#table_list").footerData('set', { "orderId": '总计',
             	   "orderTypeStr": '-',
             	   "productCode": '-',
             	   "productTypeStr": '-',
             	   "operatorCodeStr": '-',
             	   "areaCodeStr": '-',
             	   "salePriceStr": sale_price,
             	   "productCostStr": product_cost,
             	   "quantityStr": quantity,
             	   "remainQuantityStr": remain_quantity,
             	   "clientId": '-',
             	   "agentId": '-',
             	   "statusStr": '-',
             	   "activePeriodStr": '-',
             	   "endTimeStr": '-',
             	   "createTimeStr": '-',
             	   "total": '-' } );  
                }
        });

        $(window).bind("resize", function () {
            var width = $(".jqGrid_wrapper").width();
            $("#table_list").setGridWidth(width);
        })

        function keyUp(e) {
            var currKey=0,e=e||event;
            currKey=e.keyCode||e.which||e.charCode;
            var keyName = String.fromCharCode(currKey);
            if (currKey == 13){
                search();
            }
        }
        document.onkeyup = keyUp;
    });
</script>

</body>
</html>